﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using HNG.LIB.Controls;
public partial class AdminPortal_Controls_SanPham_PhuKien_list : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    TblSanPhamPhuKienManager _TblSanPhamPhuKienManager = new TblSanPhamPhuKienManager();
    public int PageSize
    {
        get { return Paging1.PageSize; }
        set { Paging1.PageSize = value; }
    }

    protected int _StartIndex;
    public int StartIndex
    {
        get { return _StartIndex; }
        set { _StartIndex = value; }
    }

    public bool Paging
    {
        get { return Paging1.Visible; }
        set { Paging1.Visible = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Paging1.CurrentPage = 1;
            FunctionExtend.LoadPageSize(ddlPageSize);
            LoadDataToGrid(new TblSanPhamPhuKienEntity());
        }
    }

    private void LoadDataToGrid(TblSanPhamPhuKienEntity _TblSanPhamPhuKienEntity)
    {
        Paging1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
        _TblSanPhamPhuKienEntity.LangId = LangID;
        _TblSanPhamPhuKienEntity.StartIndex = (Paging1.CurrentPage - 1) * Paging1.PageSize;
        _TblSanPhamPhuKienEntity.EndIndex = Paging1.CurrentPage * Paging1.PageSize;
        if (Paging1.CurrentPage > 1) _TblSanPhamPhuKienEntity.StartIndex += 1;
        DataTable _dt = _TblSanPhamPhuKienManager.be_SelectToGridview(_TblSanPhamPhuKienEntity);

        if (_dt != null)
        {
            if (_dt.Rows.Count > 0)
            {
                Paging1.TotalItems = Convert.ToInt32(_dt.Rows[0]["TotalItems"].ToString());
            }
            lblTotalRecord.Text = _dt.Rows.Count.ToString();
            dgvListPhuKien.DataSource = _dt;
            dgvListPhuKien.DataBind();
        }
        if (Paging1.Visible) Paging1.BeginPaging();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/PhuKienAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvListPhuKien.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                try
                {
                    _TblSanPhamPhuKienManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblSanPhamPhuKienEntity());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblSanPhamPhuKienEntity _TblSanPhamPhuKienEntity = new TblSanPhamPhuKienEntity();
        _TblSanPhamPhuKienEntity.Condition = txtKeySearch.Text;
        Paging1.CurrentPage = 1;
        LoadDataToGrid(_TblSanPhamPhuKienEntity);

    }
    protected void dgvListPhuKien_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkPhuKien = (HyperLink)e.Row.FindControl("lnkPhuKien");

        Image imgUpdate = (Image)e.Row.FindControl("imgUpdate");

        Label lblGiaBan = (Label)e.Row.FindControl("lblGiaBan");

        if (r != null && lnkPhuKien != null && imgUpdate != null)
        {
            lnkPhuKien.Text = r[TblSanPhamPhuKienFields.TenPhuKien.Name].ToString();
            lnkPhuKien.NavigateUrl = "~/AdminPortal/Pages/PhuKienEdit.aspx?id=" + r[TblSanPhamPhuKienFields.Id.Name].ToString();

            if (r[TblSanPhamPhuKienFields.TrangThai.Name].ToString().Equals("True"))
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_yes.png";
            else
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_no.png";
            imgUpdate.Attributes.Add("onclick", "javascript:changeStatusPhuKien('" + imgUpdate.ClientID + "','" + r[TblSanPhamPhuKienFields.Id.Name].ToString() + "')");

            lblGiaBan.Text = Convert.ToDouble(r[TblSanPhamPhuKienFields.GiaBan.Name].ToString()).ToString("#,###");

        }
    }

    protected void dgvListPhuKien_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ListSanPham")
        {
            TblSanPhamPhuKienEntity _TblSanPhamPhuKienEntity = new TblSanPhamPhuKienEntity();
            _TblSanPhamPhuKienEntity.SanPhamId =Convert.ToInt32( e.CommandArgument.ToString());
            Paging1.CurrentPage = 1;
            LoadDataToGrid(_TblSanPhamPhuKienEntity);
        }
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        Paging1.CurrentPage = 1;
        LoadDataToGrid(new TblSanPhamPhuKienEntity());
    }
    protected void Paging1_OnPageIndexChanged(object sender, EventArgs e)
    {
        LoadDataToGrid(new TblSanPhamPhuKienEntity());
        Paging1.BeginPaging();
    }
}